#include "attitudepanel.h"
#include "qpainter.h"
#include "math.h"
#include "colorname.h"

AttitudePanel::AttitudePanel(QWidget* parent):
    QWidget(parent)
{

}

void AttitudePanel::paintEvent(QPaintEvent *)
{
    int width = this->width();
    int height = this->height();
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width/2, height/2);
    int side = qMin(width, height);
    painter.scale(side / 200.0, side / 200.0);

    drawBackPanel(&painter);
    drawRefer(&painter);
    drawRollLines(&painter);

}

void AttitudePanel::drawBackPanel(QPainter *painter)
{
    float pitch = this->pitch;
    float roll = this->roll;

    if(pitch>30) pitch = 30.0f;
    if(pitch<-30) pitch = -30.0f;
    float theta = asin(-pitch/30.0)*57.3;

    painter->save();
    painter->rotate(-roll);
    painter->setPen(Qt::NoPen);
    //天
    painter->setBrush(QColor(157,195,230,255));
    painter->drawChord(-90,-90,180,180,theta*16,(90-theta)*2*16);
    //地
    painter->setBrush(QColor(244,177,131,255));
    painter->drawChord(-90,-90,180,180,(180-theta)*16,(180+theta*2)*16);

    QPen pen;
    pen.setColor(QColor(34,42,53,255));
    pen.setWidthF(2.0);
    painter->setPen(pen);
    QFont font = painter->font();
    font.setPixelSize(12);
    font.setFamily("微软雅黑");
    font.setBold(true);
    font.setStyleStrategy(QFont::ForceOutline);
    painter->setFont(font);

    //长线
    for(int i=-2;i<3;i++)
    {
        painter->drawLine(-20,(i*10+pitch)*3,20,(i*10+pitch)*3);
        painter->drawText(-45,(i*10+pitch)*3-10,20,20,Qt::AlignCenter,QString("%1").arg(-i*10),Q_NULLPTR);
        painter->drawText(25,(i*10+pitch)*3-10,20,20,Qt::AlignCenter,QString("%1").arg(-i*10),Q_NULLPTR);

    }

    //短线
    for(int i=-2;i<2;i++)
    {
        painter->drawLine(-10,(i*10+5+pitch)*3,10,(i*10+5+pitch)*3);
    }

    QPointF points[3]={
        QPointF(0.0,-68.0),
        QPointF(-5.0,-58.0),
        QPointF(5.0,-58.0)
    };
    painter->setPen(Qt::NoPen);
    painter->setBrush(DpPepple);
    painter->drawPolygon(points,3);
    painter->restore();
}

void AttitudePanel::drawRefer(QPainter *painter)
{
    painter->save();
    QPen pen;
    pen.setColor(QColor(255,255,255,255));
    pen.setWidthF(3.0);
    painter->setPen(pen);
    painter->drawLine(-60,0,-30,0);
    painter->drawLine(-30,0,-30,5);
    painter->drawLine(60,0,30,0);
    painter->drawLine(30,0,30,5);
    painter->drawPoint(0,0);
    painter->restore();
}

void AttitudePanel::drawRollLines(QPainter *painter)
{
    painter->save();
    QPen pen;
    pen.setColor(QColor(255,255,255,255));
    pen.setWidthF(5.0);
    painter->setPen(pen);
    painter->drawLine(0,-87,0,-80);
    for(int i=0;i<3;i++)
    {
        painter->rotate(30);
        painter->drawLine(0,-87,0,-80);
    }
    painter->rotate(-90);
    for(int i=0;i<3;i++)
    {
        painter->rotate(-30);
        painter->drawLine(0,-87,0,-80);
    }
    painter->rotate(60);
    pen.setWidthF(3.0);
    painter->setPen(pen);
    for(int i=0;i<5;i++)
    {
        painter->rotate(10);
        painter->drawLine(0,-88,0,-83);
    }
    painter->restore();
}

void AttitudePanel::setAttitudeValue(float pitch,float roll)
{
    this->pitch = pitch;
    this->roll = roll;
}
